package rootkit

/*
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>

#include <string.h>
#include <stdbool.h>

#define UDP_PROTO 0
#define TCP_PROTO 1
bool conn_port(int proto, int port) {
	int _fd;

	// 检查ipv4
	if (proto == UDP_PROTO) {
		if ((_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
			return false;
		}
	} else if (proto == TCP_PROTO) {
		if ((_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
			return false;
		}
	} else {
		return false;
	}
	struct sockaddr_in server;
	memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    server.sin_addr.s_addr = htonl(INADDR_ANY);
	if (bind(_fd, (struct sockaddr *)&server, sizeof(server)) < 0) {
		close(_fd);
		return true;
	}
	close(_fd);

	// 检查ipv6
	if (proto == UDP_PROTO) {
		if ((_fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
			return false;
		}
	} else if (proto == TCP_PROTO) {
		if ((_fd = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP)) < 0) {
			return false;
		}
	} else {
		return false;
	}
	struct sockaddr_in6 server6;
	memset(&server6, 0, sizeof(server6));
    server6.sin6_family = AF_INET6;
    server6.sin6_port = htons( port );
    memcpy(&server6.sin6_addr.s6_addr, &in6addr_any, sizeof in6addr_any);
    if (bind(_fd, (struct sockaddr *)&server6, sizeof(server6)) < 0) {
    	// bind失败，说明该端口已经打开
    	close(_fd);
    	return true;
    }
    close(_fd);
	return false;
}

 */
import "C"
import (
	"fmt"
	"log"
	"time"
)

//const (
//	UDPProto = iota
//	TCPProto
//)

func main3()  {
	begin := time.Now().Unix()
	var t int
	var u int
	for i := 0; i <= 65535; i++ {
		if bool(C.conn_port(C.int(UDPProto), C.int(i))) {
			log.Println(fmt.Sprintf("UDP: %d", i))
			u++
		}

		if bool(C.conn_port(C.int(TCPProto), C.int(i))) {
			log.Println(fmt.Sprintf("TCP: %d", i))
			t++
		}
	}
	end := time.Now().Unix()
	log.Println("udp:", u)
	log.Println("tcp:", t)
	log.Println(fmt.Sprintf("end-begin: %d", end - begin))
	return
}